import { makeAutoObservable } from "mobx"

import { login as loginApi } from "@/api/api"

import { getToken, setToken, clearToken } from "@/utils/token"

class LoginStore {
  token = getToken() || ""
  constructor() {
    makeAutoObservable(this)
  }
  login (params) {
    let { username, password } = params
    let temp = {
      mobile: username,
      code: password
    }
    return new Promise(async (resolve, reject) => {
      const { data } = await loginApi(temp)
      this.token = data.token
      setToken(this.token)
      resolve()
    })
  }
  logout () {
    this.token = ""
    clearToken()
  }
}

export default LoginStore
